#!/bin/bash

function getJsonValue(){
	grepKey=$1
	grepFile=$2

	cat $grepFile  |jq "${grepKey}"| sed 's/"//g'

}

## 通过dmidecode 获取服务器信息
function main(){
	psuInfoFile='/tmp/dmidecode.psu.json'
	lshw -c power -json >$psuInfoFile

	for id in {0..3};
	do
		## psu id
		returnValue=`getJsonValue ".[${id}].physid" $psuInfoFile`
		if [[ $? -ne 0 ]] || [[ "$returnValue" == 'null' ]];then
			continue
		fi

		echo -n "physid=${returnValue};"

		## psu manufacturer
		returnValue=`getJsonValue ".[${id}].vendor" $psuInfoFile`
		echo -n "manufacturer=${returnValue};"

		## psu model
		returnValue=`getJsonValue ".[${id}].description" $psuInfoFile`
		echo -n "model=${returnValue};"

		## psu sn
		returnValue=`getJsonValue ".[${id}].serial" $psuInfoFile`
		echo -n "sn=${returnValue};"

		## psu vendor
		returnValue=`getJsonValue ".[${id}].capacity" $psuInfoFile`
		echo -n "capacity=${returnValue};"

		## psu version
		returnValue=`getJsonValue ".[${id}].version" $psuInfoFile`
		echo -n "version=${returnValue};"

		echo "category=PSU"

	done
}

main